<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Implicit objects in JSPs</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="jee tutorial, java, web applications, implicit objects">
<meta name="description" content="This part of the Java EE 5 tutorials covers implicit objects in JSP.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href=".." title="Contents">Contents</a>


<h1>Implicit objects in JSPs</h1>

<p>
In this part of the JEE tutorials, we will talk about implicit objects in 
JSP pages. Implicit objects are important
objects, that are available within scriptlets. Here we will mention some of them.
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 


<h2>out object</h2>

<p>
Implicit object out is used to write to the output stream. The out object 
has page scope. A specific out object is 
available for each JSP page. 
</p>

<div class="codehead">index.jsp</div>
<pre class="code">
&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
&lt;%@page import="java.util.List" %&gt;
&lt;%@page import="java.util.ArrayList" %&gt;


&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Implicit Out Object&lt;/title&gt;
      &lt;style&gt;
          * { font-size: 12px; font-family: Verdana }
          td { border: 1px solid #ccc; padding: 3px }
          th { border: 1px solid #4B8699; padding: 3px; 
              background: #4B8699; color: white }
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h2&gt;Out Object&lt;/h2&gt;

      &lt;%!
          List authors = new ArrayList();
      %&gt;

      &lt;%
          if (authors.isEmpty()) { 
            authors.add("Leo Tolstoy");
            authors.add("Fiodor Dostoevsky");
            authors.add("Emile Zola");
            authors.add("Honore de Balzac");
          }

          out.println("&lt;table&gt;");
          out.println("&lt;th&gt;");
          out.println("Authors");
          out.println("&lt;/th&gt;");

          for (int i = 0; i &lt; authors.size(); i++) {
              out.println("&lt;tr&gt;");
              out.println("&lt;td&gt;");
              out.println(authors.get(i));
              out.println("&lt;/td&gt;");
              out.println("&lt;/tr&gt;");
          }
          out.println("&lt;/table&gt;");

      %&gt;

  &lt;/body&gt;
&lt;/html&gt;
</pre>


<p>
Our code example creates a html table. It uses the implicit out parameter. 
</p>

<pre class="explanation">
 out.println("&lt;th&gt;");
 out.println("Authors");
 out.println("&lt;/th&gt;");
</pre>

<p>
This code prints a header for the table.
</p>


<p>
There is another way, how we can print the table. By mixing html and java code. 
</p>

<div class="codehead">index.jsp</div>
<pre class="code">
&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
&lt;%@page import="java.util.List" %&gt;
&lt;%@page import="java.util.ArrayList" %&gt;


&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Implicit Out Object&lt;/title&gt;
        &lt;style&gt;
            * { font-size: 12px; font-family: Verdana }
            td { border: 1px solid #ccc; padding: 3px }
            th { border: 1px solid #4B8699; padding: 3px; 
                background: #4B8699; color: white }
        &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h2&gt;Out Object&lt;/h2&gt;

        &lt;%!
           List authors = new ArrayList();
        %&gt;

        &lt;%
            if (authors.isEmpty()) {
              authors.add("Leo Tolstoy");
              authors.add("Fiodor Dostoevsky");
              authors.add("Emile Zola");
              authors.add("Honore de Balzac");
            }
        %&gt;

        &lt;table&gt;
        &lt;th&gt;Authors&lt;/th&gt;

        &lt;% for (int i = 0; i &lt; authors.size(); i++) { %&gt;
          &lt;tr&gt;
            &lt;td&gt;
              &lt;%= authors.get(i) %&gt;
           &lt;/td&gt;
          &lt;/tr&gt;
        &lt;%   } %&gt;

        &lt;/table&gt;

    &lt;/body&gt;
&lt;/html&gt;
</pre>

<p>
The outcome is the same for both examples. 
</p>

<img src="/img/java/jee/outobject.png" alt="Implicit out object">
<div class="figure">Figure: Implicit out object</div>


<h2>response object</h2>

<p>
Object response is used as a reply to the request from the client. This object 
is seldom used by JSP authors. The response object has page scope. There is 
one situation, where the response object might be useful. Say we want to rename 
the url of one page. But we realize, that many people might have bookmarked the page. 
To overcame this issue, we might use the response object to redirect the request 
to a new page. 
</p>

<div class="codehead">index.jsp</div>
<pre class="code">
&lt;%
  response.sendRedirect("target.jsp");
%&gt;
</pre>

<div class="codehead">target.jsp</div>
<pre class="code">
&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Target JSP Page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h2&gt;Welcome&lt;/h2&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre>

<p>
When we run the example, we end up with the target.jsp page. 
</p>


<h2>request object</h2>

<p>
We will demonstrate a request object in a simple form. The form is use 
to send user data to the server for processing. 
</p>

<div class="codehead">index.jsp</div>
<pre class="code">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Form&lt;/title&gt;
&lt;style&gt;
* { font-size: 12px; font-family: Verdana }
input { border: 1px solid #ccc }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;form method="get" action="book.jsp"&gt;
&lt;p&gt;
Enter a book name
&lt;/p&gt;
&lt;input type="text" name="bookname"&gt;
&lt;input type="submit" value="submit"&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>


<div class="codehead">book.jsp</div>
<pre class="code">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Book&lt;/title&gt;
&lt;style&gt;
* { font-size: 12px; font-family: verdana }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;p&gt;
You have entered: &lt;%= request.getParameter("bookname") %&gt;
&lt;/p&gt;


&lt;/body&gt;
&lt;/html&gt;
</pre>

<p>
In this example, we have two jsp files. One file is used to enter data. 
We use a form. The data is sent to the second jsp file, which will simply 
print the data, we have entered. 
</p>

<pre class="explanation">
&lt;style&gt;
* { font-size: 12px; font-family: Verdana }
input { border: 1px solid #ccc }
&lt;/style&gt;
</pre>

<p>
We use some css to make our page prettier. We change the font size and 
family and set a new border for an input html tag.
</p>

<pre class="explanation">
&lt;form method="get" action="book.jsp"&gt;
</pre>

<p>
The action parameter specifies the name of the jsp file, which 
will receive the data we send. 
</p>

<pre class="explanation">
You have entered: &lt;%= request.getParameter("bookname") %&gt;
</pre>

<p>
This line will output the data we entered into the form. 
The request object encapsulates the data sent by the client.
</p>

<p>
Next we use an alternative method to deploy the war file. 
</p>

<pre class="code">
$ ls
books.jsp  index.jsp
</pre>

<p>
We have two jsp files.
</p>


<pre class="code">
$ jar -cvf form.war *
added manifest
adding: books.jsp(in = 197) (out= 151)(deflated 23%)
adding: index.jsp(in = 308) (out= 204)(deflated 33%)
</pre>

<p>
We create a web archive.
</p>

<pre class="code">
$ ls
books.jsp  form.war  index.jsp
</pre>

<p>
The web archive has been created. 
</p>

<pre class="code">
$ asadmin deploy --user=admin ~/programming/jee/form/form.war
</pre>

<p>
We can use the <b>asadmin</b> tool to deploy the web archive. 
We provide the user name and the path
to the war file. By default, the admin user is called admin 
and the password is adminadmin.
</p>

<pre class="code">
Command deploy executed successfully.
</pre>

<p>
If everything goes ok, we see this message.
</p>


<h2>application object</h2>

<p>
The <b>application</b> object is available to all clients. 
It shares data on all application pages. The application object can be used 
to share data, log messages, track user hits or access server information.
</p>

<div class="codehead">book.jsp</div>
<pre class="code">
&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;

&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;application object&lt;/title&gt;
      &lt;style&gt;
          * { font-size: 12px; font-family: Verdana }
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h2&gt;application object&lt;/h2&gt;

      &lt;p&gt;
          Resource paths:
      &lt;/p&gt;
      &lt;%= application.getResourcePaths("/")  %&gt; &lt;br&gt;
      &lt;%= application.getResourcePaths("/WEB-INF")  %&gt;&lt;br&gt;
      &lt;%= application.getResourcePaths("/META-INF")  %&gt;

      &lt;p&gt;
          Server:
      &lt;/p&gt;
      &lt;%= application.getServerInfo()  %&gt;

      &lt;p&gt;
          Context path:
      &lt;/p&gt;
      &lt;%= application.getContextPath()  %&gt;

  &lt;/body&gt;
&lt;/html&gt;
</pre>

<p>
In our example, we print resource paths. These are all paths to resources 
within the web application. Further we
print server information and the context path. Our server is Resin 3.1.3. 
The context path uniquely identifies the web application in server.
</p>

<img src="/img/java/jee/application.png" alt="application object">
<div class="figure">Figure: application object</div>


<p>
In this chapter, we mentioned JSP implicit objects.
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br> 


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified February 13, 2008  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>


